package jsu.blogger.mapper;

import jsu.blogger.pojo.Users;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UsersMapper {
    @Insert("INSERT INTO blog_system_database.users(username, password, current_device) " +
            "VALUES (#{username}, #{password}, #{currentDevice})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    void insertUser(Users user);

    @Select("select * from blog_system_database.users where username=#{username}")
    @Results({
            @Result(property = "userId",column = "user_id"),
            @Result(property = "currentDevice",column = "current_device")
    })
    Users selectUsersByUsername(String username);

    @Update("update blog_system_database.users set current_device=#{currentDevice} where user_id=#{UId}")
    void updateUserCurrentDeviceByUId(@Param("currentDevice") String currentDevice,@Param("UId")Long UId);

    @Select("select * from blog_system_database.users where user_id=#{UId}")
    @Results({
            @Result(property = "userId",column = "user_id"),
            @Result(property = "currentDevice",column = "current_device")
    })
    Users selectUserByUId(Long UId);

    @Select("select password from blog_system_database.users where user_id=#{UId}")
    String selectPasswordByUId(Long UId);

    @Update("update blog_system_database.users set username=#{username} where user_id=#{userId}")
    void updateUsernameByUserId(@Param("username") String username,@Param("userId") Long userId);

    @Select("select blog_system_database.users.username from blog_system_database.users where user_id=#{userId}")
    String selectUsernameByUserId(Long userId);

    @Select("select user_id,username from blog_system_database.users where " +
            "username like CONCAT('%', #{searchContent}, '%') limit #{begin}, #{pageSize}")
    @Results({
            @Result(property = "userId",column = "user_id"),
            @Result(property = "currentDevice",column = "current_device")
    })
    List<Users> selectUserPageBySearchContent(@Param("searchContent") String searchContent,
                                                           @Param("begin") int begin,
                                                           @Param("pageSize") int pageSize);
}
